﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Text.RegularExpressions;
using Code.Models;

namespace Code.BLL.Arithmetic
{
    /// <summary>
    /// 拆分方案中的所有注
    /// </summary>
    internal class SplitNumber
    {
        /// <summary>
        /// 从方案中获取每一注的详情
        /// </summary>
        /// <param name="lotteryType">当前彩种</param>
        /// <param name="userChoseNum">用户选择的号码 格式为[(PlayTypeID,int)(number1)]+[(LotteryTypeID,PlayTypeID)(number2)]+...。其中PlayTypeID 为玩法编号，int 表示类型(1 为复式，0 为单式),number1,和number2为用户所投注的内容。如 [(1,1)(--425)]+[(1,3)(----5)]+...</param>
        /// <returns></returns>
        public static List<UserChoseNumberHelper> GetNumList(LotteryType lotteryType, string userChoseNum,string openCode)
        {
            List<UserChoseNumberHelper> numList = new List<UserChoseNumberHelper>();
            string[] strList = userChoseNum.Split('+');//按‘+’分开号码

            foreach (var item in strList)
            {
                try
                {
                    string str = item.Substring(1, item.Length - 2);//去除[]

                    int index1 = str.IndexOf(")");//获取第一个‘）’的位置

                    string LottertyTypeIDAndPlayTypeID = str.Substring(1, index1 - 1);//获取彩种编号和玩法编号的字符串

                    string[] LP = LottertyTypeIDAndPlayTypeID.Split(',');

                    int PlayTypeID = int.Parse(LP[0]);//获取玩法编号
                    short type = short.Parse(LP[1]);//获取类型标识
                    string num = str.Substring(index1 + 2, str.Length - index1 - 3);//获取用户投注内容

                    num = LotteryFactory.CreateLottery(lotteryType).GetPlayType((Code.Models.PlayType_E)PlayTypeID).RegistNum(num);
                    if (num == null)
                        throw new ArgumentException("号码格式不正确");
                    UserChoseNumberHelper nums = new UserChoseNumberHelper();
                    nums.PlayTypeID = PlayTypeID;
                    nums.Type = type;
                    nums.Number = num;
                    numList.Add(nums);
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            return numList;
        }
        /// <summary>
        /// 日志记录对象
        /// </summary>
        private static ILog Log = Code.Commen.Log.GetLog(typeof(SplitNumber));

    }
    /// <summary>
    /// 用户投注的内容
    /// </summary>
    internal class UserChoseNumberHelper
    {
        /// <summary>
        /// 玩法的编号
        /// </summary>
        public int PlayTypeID
        { get; set; }
        /// <summary>
        /// 号码的类型，1为复式，2为单式，
        /// </summary>
        public short Type
        { get; set; }
        /// <summary>
        /// 用户选择的号码
        /// </summary>
        public string Number
        { get; set; }
    }
}
